package com.zibo.convalesce.common.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        HttpSession session = request.getSession();
        Object user = session.getAttribute("username");
        StringBuffer requestURL = request.getRequestURL();
        if (user == null) {
            logger.info("拦截器拦截...preHandle:::null:::{}",requestURL);
            response.sendRedirect("/404");
            return false;
        } else {
            logger.info("preHandle:::{}, :::{}",user ,requestURL);
            return true;
        }
    }
}
